/* The example shows
*/
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import java.io.* ;
import java.net.* ;

public class DataExchangeClient extends JFrame implements ActionListener {

   JButton btnCmd = new JButton("Start") ;
   String strJLabel[] = {"Host Address", "Port", "To Host" };
   String strText[] = {"136.168.201.110", "8900", "1" };
   JLabel lb[] = new JLabel[3];
   JPanel pl = new JPanel() ;
   JTextField tf[] = new JTextField[3];
   JTextArea  ta = new JTextArea(40, 70) ;

   SocketReadThread clientReader  = null;

   Socket exchSocket = null ;

   DataExchangeClient() {
	Container c = getContentPane();

	for ( int i = 0; i < lb.length; i ++ ) {
		lb[i] = new JLabel(strJLabel[i]) ;
		tf[i] = new JTextField(strText[i]);
		pl.add(lb[i]); pl.add(tf[i]); }

	btnCmd.addActionListener(this) ;
	pl.add(btnCmd); 
	c.add(pl, BorderLayout.SOUTH) ;
	ta.setFont(new Font("MONOSPACED", Font.PLAIN, 12) );
	c.add(new JScrollPane(ta), BorderLayout.CENTER) ;
	setTitle("Data Exchange Client Program") ;
	setSize(600, 400); show() ;
   }

   public void actionPerformed(ActionEvent e ) {
	try {
		exchSocket = new Socket(tf[0].getText(), Integer.parseInt(tf[1].getText()) ); 

		SocketReadThread readThread = new SocketReadThread ( exchSocket, ta );
		SocketWriteThread writeThread = new SocketWriteThread ( exchSocket );
		readThread.start();
		writeThread.start();

		readThread.join();
		writeThread.join();
	
		exchSocket.close();

	} catch(IOException er) { er.toString() ; }
	  catch(InterruptedException er2) { }

   }

   public static void main( String arg[] ) {
	JFrame fm =  new DataExchangeClient() ;
	fm.addWindowListener( new WindowAdapter () {
		public void windowClosing(WindowEvent e) {
			System.exit(0); } } );
  }
}
